package com.neuedu.xkxt.controller;

import com.neuedu.xkxt.dao.CourseMapper;
import com.neuedu.xkxt.dto.CourseDTO;
import com.neuedu.xkxt.model.Course;
import com.neuedu.xkxt.model.Student;
import com.neuedu.xkxt.model.Teacher;
import com.neuedu.xkxt.result.R;
import com.neuedu.xkxt.service.CourseService;
import com.neuedu.xkxt.service.UserService;
import com.neuedu.xkxt.vo.CourseVO;
import com.neuedu.xkxt.vo.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@CrossOrigin
@RestController
@RequestMapping("teacher")
public class TeacherController {

    @Resource
    private UserService userService;

    @Resource
    private CourseService courseService;
    @Autowired
    private CourseMapper courseMapper;

    @GetMapping("page")
            public R page(@RequestParam int pageNo, @RequestParam int pageSize,@RequestParam String searchKey){
                PageVO<CourseVO> page =userService.findPage(searchKey, pageNo, pageSize);
                return new R(200,"分页查询成功",page);

            }


            @PostMapping("addCourse")
            public R addCourse(@RequestBody CourseDTO courseDTO){
                String name=courseDTO.getName();
                String tname=courseDTO.getTname();
                Integer maxLimit=courseDTO.getMaxLimit();
                Teacher teacher=userService.findUserByTeacherName(tname);
                if(teacher==null){
                    return new R(500,"教师不存在",null);
                }else{
                    Course course=new Course();
                    course.setName(name);
                    course.setTid(teacher.getId());
                    course.setMaxLimit(maxLimit);
                    int result=courseService.addCourse(course);
                    if(result>0){
                        return new R(200,"新增成功",null);
                    }else{
                        return new R(501,"新增失败",null);
                    }
                }
            }
            @PostMapping("editCourse")
            public R editCourse(@RequestBody CourseDTO courseDTO){
                Integer id=courseDTO.getId();
                String name=courseDTO.getName();
                String tname=courseDTO.getTname();
                Integer maxLimit=courseDTO.getMaxLimit();
                Teacher teacher=userService.findUserByTeacherName(tname);
                if(teacher==null){
                    return new R(500,"教师不存在",null);
                }else{
                    Course course=new Course();
                    course.setId(id);
                    course.setName(name);
                    course.setTid(teacher.getId());
                    course.setMaxLimit(maxLimit);
                    int result=courseService.updateCourse(course);
                    if(result>0){
                        return new R(200,"修改成功",null);
                    }else{
                        return new R(501,"修改失败",null);
                    }
                }
            }

            @GetMapping("delCourse")
            public R delCourse(@RequestParam Integer id){
            int result=courseService.deleteCourse(id);
            if(result>0){
                return new R(200,"删除成功",null);
            }else {
                return new R(501,"删除失败",null);
            }
    }
            @GetMapping("pageByCourseId")
            public R pageByCourseId(@RequestParam int pageNo, @RequestParam int pageSize,@RequestParam int courseId){
                PageVO<Student> page=courseService.page(pageNo,pageSize,courseId);
                return new R(200,"查询成功",page);
            }
            @GetMapping("delStudentWithCourse")
            public R delStudentWithCourse(@RequestParam Integer stuId,@RequestParam Integer courseId){
                int result=courseService.delStudentCourseRel(courseId,stuId);
                if(result>0){
                    return new R(200,"删除成功",null);
                }else {
                    return new R(501,"删除失败",null);
                }
    }
}
